//
//  XinMainVC.swift
//  XinGuoSearch
//
//  Created by 李东 on 2024/4/8.
//

import UIKit
import SnapKit

class XinMainVC: XinBaseVC {
    
    lazy var scrollView = UIScrollView()
    lazy var lineView = UIView()
    lazy var setupBtn = UIButton(type: .custom)
    lazy var topImgView = UIImageView(image: UIImage(named: "img_home_top"))
    lazy var centerImgView = UIImageView(image: UIImage(named: "img_home_camera"))
    lazy var itemBtn1 = UIButton(type: .custom)
    lazy var itemBtn2 = UIButton(type: .custom)

    override func viewDidLoad() {
        super.viewDidLoad()

        hideNavgaitionBar()
//        showBgImgView = true
        configUI()
        NotificationCenter.default.addObserver(self, selector: #selector(showAd), name: .SDKInitFinish, object: nil)
    }
    
    @objc func showAd() {
        if AppProducts.shared.isPurchased {
            adHelper.removeBannerView()
        }else{
            if hasLoadAd {
                return
            }
            hasLoadAd = true
            adHelper.loadBanner(currentVC: self, isMainVC: false)
        }
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        if ADManager.initFinish {
            showAd()
        }
    }
    
    func configUI() {
        scrollView.contentInsetAdjustmentBehavior = .never
        view.addSubview(scrollView)
        scrollView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        lineView.backgroundColor = .clear
        scrollView.addSubview(lineView)
        lineView.snp.makeConstraints { make in
            make.left.top.right.equalToSuperview()
            make.height.equalTo(1)
            make.width.equalTo(view)
        }
        
        setupBtn.setImage(UIImage(named: "img_setup"), for: .normal)
        setupBtn.addTarget(self, action: #selector(setupAction), for: .touchUpInside)
        scrollView.addSubview(setupBtn)
        setupBtn.snp.makeConstraints { make in
            make.top.equalTo(CGFloat.topPadding)
            make.right.equalTo(-10)
            make.width.height.equalTo(44)
        }
        
        scrollView.addSubview(topImgView)
        topImgView.snp.makeConstraints { make in
            make.left.equalTo(20)
            make.top.equalTo(setupBtn.snp.bottom)
        }
        
        scrollView.addSubview(centerImgView)
        centerImgView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(topImgView.snp.bottom).offset(25)
        }
        
        itemBtn1.setImage(UIImage(named: "img_home_item1"), for: .normal)
        itemBtn1.addTarget(self, action: #selector(itemAction(sender:)), for: .touchUpInside)
        scrollView.addSubview(itemBtn1)
        itemBtn1.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(centerImgView.snp.bottom).offset(25)
        }
        
        itemBtn2.setImage(UIImage(named: "img_home_item2"), for: .normal)
        itemBtn2.addTarget(self, action: #selector(itemAction(sender:)), for: .touchUpInside)
        scrollView.addSubview(itemBtn2)
        itemBtn2.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(itemBtn1.snp.bottom).offset(30)
            make.bottom.equalToSuperview().offset(-120)
        }
    }


    @objc func setupAction() {
        navigationController?.pushViewController(XinMeVC(), animated: true)
    }
    
    @objc func itemAction(sender: UIButton) {
        if !AppProducts.shared.isPurchased {
            showSubVC()
            return
        }
        if sender == itemBtn1 {
            navigationController?.pushViewController(XinWebVC(url: AppConfig.H5_URL), animated: true)
        }else {
            navigationController?.pushViewController(XinCoffeeVC(), animated: true)
        }
    }

}
